package com.example.owner.lettery1.adapter

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.owner.lettery1.R
import com.example.owner.lettery1.modlus.BaseImp
import com.example.owner.lettery1.ui.OpenLotteryRoomFragment
import com.example.owner.lettery1.utils.DialogUtil


abstract class BaseRecyclerViewFragment<P> : Fragment(), BaseImp<P> {

    var presenter: P? = null

    var appActivity: Activity? = null

    var rootView : View? = null
    var fixTitle : View? = null

    private var swipeRefreshLayout : SwipeRefreshLayout? = null
    var recyclerView : RecyclerView? = null

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        appActivity = context as Activity?
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if(rootView == null){
            rootView = inflater.inflate(R.layout.recycler_view,container,false)
            presenter = onCreatePresenter()
            onInitView()
            onLoadData()
            setupListener()

        }
        return rootView
    }

    override fun onInitView() {
        recyclerView = rootView!!.findViewById(R.id.recyclerView)
        swipeRefreshLayout = rootView!!.findViewById(R.id.swipeRefreshLayout)
        fixTitle = rootView!!.findViewById(R.id.fixTitle)
        fixTitle!!.visibility = View.GONE
    }


    fun setFixView(visitily : Int){
        fixTitle!!.visibility = visitily
    }

    override fun setupListener() {
        recyclerView!!.layoutManager = LinearLayoutManager(appActivity)
        swipeRefreshLayout!!.setOnRefreshListener {
            onRefresh()
        }
    }


    private fun onRefresh() {
        OpenLotteryRoomFragment
        onLoadData()
    }

    open fun showOnLoading() {
        DialogUtil.showProgressDialog(fragmentManager, appActivity)
        swipeRefreshLayout!!.isRefreshing = true
    }

    open fun hideOnLoading() {
        DialogUtil.hideProgressDialog(fragmentManager)
        swipeRefreshLayout!!.isRefreshing = false
    }

}